package jdbc;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

/*
事务
 */
public class JDBCDemo9 {
    public static void main(String[] args) {
        try ( Connection connection=DBUtil.getConnection()){
            connection.setAutoCommit(false);
            Statement statement=connection.createStatement();
            String sql="INSERT INTO userinfo(username,password,nickname,age) " +
                    "VALUES ('王五','123123','oi',25)";
            statement.executeUpdate(sql);
            System.out.println("插入成功");
            ResultSet rs = statement.executeQuery("SELECT username FROM userinfo");
            while(rs.next()){
                System.out.println(rs.getString("username"));
            }

            connection.commit();//之前的操作全部算数了
            //connection.rollback();
            //之后会开启新的事务
            //开始新的增删改操作
            //要么Commit:将上次commit之后的操作全部算数
            //要么ROLLBACK:将上次commit之后的操作全部撤销

        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }
}
